﻿using Esint.JudicialAuction.BLL;
using Esint.JudicialAuction.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebJudicialAuction.admin.bases;
using Esint.Common;
using System.IO;
using Aspose.Cells;
using Esint.JudicialAuction.Model.Common;

namespace WebJudicialAuction.admin.handler
{
    /// <summary>
    /// userManagement 的摘要说明
    /// </summary>
    public class userManagement : BaseHandler, IHttpHandler
    {
        Schedule_UserService userBll = new Schedule_UserService();
        Sys_CodeService codeBll = new Sys_CodeService();
        
        protected override AjaxResult BusinessHandleFunc(string req_type, Dictionary<string, string> dicData)
        {
            AjaxResult ajaxResult = new AjaxResult();

            switch (req_type)
            {
                default:
                    break;

                case "GetPersonList":
                    {
                        var pagedList = userBll.GetSchedulUserList(dicData["SearchUserName"], dicData["SearchIsAvailable"],dicData["Gender"], dicData["pgIndex"], dicData["pgSize"]);
                        if (pagedList != null)
                        {
                            ajaxResult.flag = AjaxResultFlag.Success;
                            ajaxResult.data = pagedList;
                        }
                        else
                        {
                            ajaxResult.flag = AjaxResultFlag.Failed;
                            ajaxResult.msg = AjaxMessage.SearchFailed;
                        }

                        break;
                    }

                case "DeleteOneUser":
                    {
                        var model = userBll.GetSchedule_UserInfo(dicData["id"]);
                        model.IsDelete = 1;
                        userBll.Update(model);
                        ajaxResult.flag = AjaxResultFlag.Success;
                        ajaxResult.msg = AjaxMessage.DeleteSuccess;

                        break;
                    }

                case "GetModelDetail":
                    {
                        var model = userBll.GetSchedule_UserInfo(dicData["id"]);
                        if (model != null)
                        {
                            ajaxResult.flag = AjaxResultFlag.Success;
                            ajaxResult.data = model;
                            ajaxResult.msg = AjaxMessage.SearchSuccess;
                        }
                        else
                        {
                            ajaxResult.flag = AjaxResultFlag.Failed;
                            ajaxResult.msg = AjaxMessage.NotFoundData;
                        }

                        break;
                    }
                    
                case "ImportUserData":
                    {
                        var fileName = dicData["fileName"];
                        var excelPath = OwnContext.Server.MapPath(CommonExtend.GetAppSetting("UploadPath")) + fileName;
                        if (!File.Exists(excelPath))
                        {
                            ajaxResult.flag = AjaxResultFlag.Failed;
                            ajaxResult.msg = AjaxMessage.NotFoundFile;
                        }
                        else
                        {
                            Workbook workbook = new Workbook(excelPath);
                            Worksheet worksheet = workbook.Worksheets["排班人员列表"];

                            var UserList = new List<Schedule_UserInfo>();
                            var rowIndex = 1;
                            while (!string.IsNullOrWhiteSpace(worksheet.Cells[rowIndex, 0].StringValue))
                            {
                                var userName = worksheet.Cells[rowIndex, 0].StringValue;
                                var GenderMeaning = worksheet.Cells[rowIndex, 1].StringValue;
                                var GenderCode = codeBll.GetCodeObjectByFlagAndMeaning("4", GenderMeaning);
                                if (GenderCode == null)
                                {
                                    throw new DataValidException($"导入失败！第{ (rowIndex+1) }行【{userName}】性别列填写有误");
                                }

                                UserList.Add(new Schedule_UserInfo() { IsDelete = 0, Priority = 0, ScheduleWeight=userBll.GetAllUserAverageWeight(), IsAvailable = 1, UserId = Guid.NewGuid().ToString(), UserName = userName, Gender=GenderCode.Code });
                                rowIndex++;
                            }

                            userBll.SaveMutli(UserList);

                            ajaxResult.flag = AjaxResultFlag.Success;
                            ajaxResult.msg = AjaxMessage.SaveSuccess;
                        }
                        
                        break;
                    }

                case "ChangeUserAvailable":
                    {
                        Schedule_UserInfo userInfo = new Schedule_UserInfo();
                        userInfo.UserId = dicData["UserId"];
                        userInfo.IsAvailable = byte.Parse(dicData["IsAvailable"]);

                        if (userInfo.IsAvailable == 1)
                        {
                            //重置权重
                            userInfo.ScheduleWeight = userBll.GetAllUserAverageWeight();
                        }

                        userBll.Save(userInfo);

                        ajaxResult.flag = AjaxResultFlag.Success;
                        ajaxResult.msg = AjaxMessage.OperationSuccess;

                        break;
                    }

                case "SaveModelInfo":
                    {
                        Schedule_UserInfo userInfo = JsonConvert.DeserializeObject<Schedule_UserInfo>(dicData["modelInfo"]);
                        
                        userBll.Save(userInfo);

                        ajaxResult.flag = AjaxResultFlag.Success;
                        ajaxResult.msg = AjaxMessage.OperationSuccess;

                        break;
                    }


            }

            return ajaxResult;
        }
    }
}